package com.masterclient.app.modules.goods.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.eoner.commonbean.product.ProductMessage;
import com.eoner.baselibrary.utils.GlideUtil;
import com.eoner.managerlibrary.router.AroutePath;
import com.masterclient.app.R;
import com.eoner.baselibrary.utils.Utils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class SellerRecommendProductAdapter extends RecyclerView.Adapter {


    private Context context;
    private List<ProductMessage> productMessages;
    String seller_id,isBoutique;


    public SellerRecommendProductAdapter(Context context, List<ProductMessage> productMessages, String seller_id, String isBoutique) {
        this.context = context;
        this.productMessages = productMessages;
        this.seller_id = seller_id;
        this.isBoutique = isBoutique;
    }

    @NonNull
    //创建新View，被LayoutManager所调用
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.seller_recommend_product_item, parent, false);
        RecyclerView.ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {


        ViewHolder viewHolder = (ViewHolder) holder;
        if (position == 4 && productMessages.size() > 5) {
            viewHolder.more.setVisibility(View.VISIBLE);
            viewHolder.line.setVisibility(View.VISIBLE);
        } else {
            viewHolder.more.setVisibility(View.GONE);
            viewHolder.line.setVisibility(View.GONE);
        }

        ProductMessage productMessage = productMessages.get(position);
        GlideUtil.displayRoundImage(context, productMessage.getImage(), viewHolder.image, 7, R.mipmap.defaults);
        viewHolder.title.setText(productMessage.getTitle());
        Utils.set_price_style(viewHolder.price, "￥" + productMessage.getPrice());
        Utils.setDin(viewHolder.price, context);

        viewHolder.product_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance().build(AroutePath.COMMODITY_DETAIL_ACTIVITY)
                        .withString("id", productMessage.getProduct_id())
                        .navigation();
            }
        });

        viewHolder.more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance().build(AroutePath.STORE_ACTIVITY).withString("store_id",seller_id).navigation();
            }
        });

    }

    @Override
    public int getItemCount() {
        return productMessages.size() > 5 ? 5 : productMessages.size();
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item)
        public View item;
        @BindView(R.id.more)
        public View more;
        @BindView(R.id.line)
        public View line;
        @BindView(R.id.product_layout)
        public View product_layout;
        @BindView(R.id.image)
        public ImageView image;
        @BindView(R.id.title)
        public TextView title;
        @BindView(R.id.price)
        public TextView price;
        @BindView(R.id.tv_money_earn)
        public TextView tv_money_earn;

        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }


}

